using System;

namespace EMan.Common
{
  public struct Id< T >
  {
    public static Id< T > Invalid = new Id< T >( int.MinValue );

    private int _value;

    public Id( int value )
    {
      _value = value;
    }

    public static explicit operator int( Id<T> id)
    {
      return id._value;
    }

    public static explicit operator Id<T>( int value )
    {
      return new Id< T >( value );
    }

    public static bool operator ==( Id<T> l, Id< T > r )
    {
      return l._value == r._value;
    }

    public static bool operator !=( Id< T > l, Id< T > r )
    {
      return l._value != r._value;
    }

    public static Id< T > operator--( Id< T > id )
    {
      id._value--;
      return id;
    }

    public static Id< T > operator++( Id< T > id )
    {
      id._value++;
      return id;
    }

    public static bool operator <( Id< T > l, int r )
    {
      return l._value < r;
    }

    public static bool operator >( Id< T > l, int r )
    {
      return l._value > r;
    }

    public override bool Equals( object obj )
    {
      var equals = false;
      try
      {
        var other = ( Id< T > )obj;
        equals = _value == other._value;
      }
      catch( InvalidCastException ){}
      return equals;
    }

    public override int GetHashCode()
    {
      return _value.GetHashCode();
    }
  }
}